package org.itest.jci.v3;

import org.itest.jci.v3.support.JdkCompiler;

import java.lang.reflect.Method;

public class DynamicInvoker {
    public static void main(String[] args) {
        String className = "org.itest.jci.FOO";

        String method = "main";

        String codes = "package org.itest.jci;\n" +
                "\n" +
                "public class FOO {\n" +
                "\n" +
                "    public static void main(String[] args) {\n" +
                "        System.out.println(\"Test JCompilerInvoker3\");\n" +
                "    }\n" +
                "}\n";

        DynamicCompiler dynamicCompiler = new JdkCompiler();
        Class<?> clazz = dynamicCompiler.compile(codes, DynamicCompiler.class.getClassLoader());
        try {
            // 生成对象
            Object obj = clazz.newInstance();
            Class<? extends Object> cls = obj.getClass();

            // 调用sayHello方法
            Method m = clazz.getMethod(method, String[].class);
            Object invoke = m.invoke(obj, new Object[]{new String[]{}});
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
